<?php

require_once '../config.php';
session_start();

require_once '../function.php';

//添加
function add_category()
{
    global $message;
    //验证表单
    if (empty($_POST['name']) || empty($_POST['slug'])) {
        $message = '请填写完整信息';
        return;
    }
    //获取表单信息
    $name = $_POST['name'];
    $slug = $_POST['slug'];

    //添加到数据库
    $rows = xiu_IDU("insert into categories values(null,'{$name}','{$slug}');");

    $message = $rows <= 0 ? '添加失败' : '添加成功';

}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    add_category();
}

//编辑
function edit_category(){
    //提升作用域
    global $current_edit_category;

    //获取需要编辑的数据
    $edit_id = $current_edit_category['id'];

    $name = empty($_POST['name']) ? $current_edit_category['name'] : $_POST['name'];
    $slug = empty($_POST['slug']) ? $current_edit_category['slug'] : $_POST['slug'];

    //获取编辑完的数据
    $current_edit_category['name'] = $name;
    $current_edit_category['slug'] = $slug;


    //正在编辑
    xiu_IDU("update categories set slug = '{$slug}', name = '{$name}' where id = {$edit_id}");


}

//通过url传参判断是添加数据还是编辑数据
if (empty($_GET['id'])){

    if ($_SERVER['REQUEST_METHOD'] === 'POST'){
        add_category();
    }

}else{

    //拿id对应的数据
    $current_edit_category =  xiu_query_one("select * from categories where id = {$_GET['id']};");


    //填写完后告诉服务器
    if ($_SERVER['REQUEST_METHOD'] === 'POST'){
        edit_category();
    }
}



//查询
$categories = xiu_query_all('select * from categories;');

?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>Categories &laquo; Admin</title>
    <link rel="stylesheet" href="/static/assets/vendors/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="/static/assets/vendors/font-awesome/css/font-awesome.css">
    <link rel="stylesheet" href="/static/assets/vendors/nprogress/nprogress.css">
    <link rel="stylesheet" href="/static/assets/css/admin.css">
    <script src="/static/assets/vendors/nprogress/nprogress.js"></script>
</head>
<body>
<script>NProgress.start()</script>

<div class="main">
    <?php include 'inc/navbar.php'; ?>

    <div class="container-fluid">
        <div class="page-title">
            <h1>分类目录 <a href="<?php echo $_SERVER['PHP_SELF'];?>">返回 < </a></h1>
        </div>
        <!-- 有错误信息时展示 -->
        <?php if (isset($message)): ?>
            <div class="alert <?php echo $message === '添加成功' ? ' alert-success' : ' alert-danger'; ?>">
                <?php echo $message; ?>
            </div>
        <?php endif; ?>
        <div class="row">
            <div class="col-md-4">

                <?php if (isset($current_edit_category)):?>
                <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
                    <h2>编辑分类目录</h2>
                    <div class="form-group">
                        <label for="name">名称</label>
                        <input id="name" class="form-control" name="name" type="text" placeholder="分类名称" value="<?php echo $current_edit_category['name'];?>">
                    </div>
                    <div class="form-group">
                        <label for="slug">别名</label>
                        <input id="slug" class="form-control" name="slug" type="text" placeholder="slug" value="<?php  echo $current_edit_category['slug'];?>">
                        <p class="help-block">https://zce.me/category/<strong>slug</strong></p>
                    </div>
                    <div class="form-group">
                        <button class="btn btn-primary" type="submit">保存</button>
                    </div>
                </form>
                <?php else:?>
                <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
                    <h2>添加新分类目录</h2>
                    <div class="form-group">
                        <label for="name">名称</label>
                        <input id="name" class="form-control" name="name" type="text" placeholder="分类名称">
                    </div>
                    <div class="form-group">
                        <label for="slug">别名</label>
                        <input id="slug" class="form-control" name="slug" type="text" placeholder="slug">
                        <p class="help-block">https://zce.me/category/<strong>slug</strong></p>
                    </div>
                    <div class="form-group">
                        <button class="btn btn-primary" type="submit">添加</button>
                    </div>
                </form>
                 <?php endif;?>
            </div>
            <div class="col-md-8">
                <div class="page-action">
                    <!-- show when multiple checked -->
                    <a class="btn btn-danger btn-sm" href="/admin/category-delete.php" style="visibility:hidden;width: 70px;" id="remove">批量删除</a>
                </div>
                <table class="table table-striped table-bordered table-hover">
                    <thead>
                    <tr>
                        <th class="text-center" width="40"><input type="checkbox" data-id="-1"></th>
                        <th>名称</th>
                        <th>Slug</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                    </thead>
                    <tbody>

                    <?php foreach ($categories as $item): ?>
                        <tr>
                            <td class="text-center"><input type="checkbox"  data-id="<?php echo $item['id']?>"></td>
                            <td><?php echo $item['name']; ?></td>
                            <td><?php echo $item['slug']; ?></td>
                            <td class="text-center">
                                <a href="categories.php?id=<?php echo $item['id'] ?>" class="btn btn-info btn-xs">编辑</a>
                                    <a href="category-delete.php?id=<?php echo $item['id'] ?>" class="btn btn-danger btn-xs">删除</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<?php $current_page = 'categories'; ?>
<?php include 'inc/sidebar.php'; ?>


<script src="/static/assets/vendors/jquery/jquery.js"></script>
<script src="/static/assets/vendors/bootstrap/js/bootstrap.js"></script>
<script>

    $(function () {
        let btn_remove = $('#remove');
        let inputAll = $('thead input');
        let input = $('tbody input');
        let tableInput = $('table input');
        let visibility = null;
        let arr = [];
        let id = null;


        tableInput.on('change', function () {
            //利用h5自定义属性data-保存每个元素的id,存放到数组,便于删除数据
            id = $(this).data('id');
            if ($(this).prop('checked')) {
                arr.push(id);
            } else {
                arr.splice(arr.indexOf(id), 1);
            }

            if ($('tbody input:checked').length === 0) {
                visibility = 'hidden';
            } else {
                visibility = 'visible';
            }

            btn_remove.css('visibility', visibility);
            //search: 搜索资源路径
            btn_remove.prop('search', '?id=' + arr);

        });

        inputAll.on('change', function () {
            input.prop('checked', $(this).prop('checked'));
            if ($('thead input:checked').length === 0) {
                visibility = 'hidden';
                arr.length = 0;
            } else {
                visibility = 'visible';
            }
            btn_remove.css('visibility', visibility);
        });


        input.on('change', function () {
            let checkedLen = $('tbody input:checked').length;
            let inputLen = input.length;
            inputAll.prop('checked', checkedLen === inputLen);
        });

    });


</script>
<script>NProgress.done()</script>
</body>
</html>
